﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MockupDesigner.AppItems.Commands
{
    public class InsertCommand : ICommand
    {

        public CommandType CommandType
        {
            get { return CommandType.Undoable; }
        }

        private DesignerCanvas designerCanvas;
        private List<DesignerItem> designerItems;

        public void Execute(object target, params object[] parameters)
        {
            if (!(target is DesignerCanvas))
            {
                throw new InvalidCastException("The target must be a DesignerCanvas");
            }
            this.designerCanvas = (DesignerCanvas)target;
            if (!(parameters[0] is List<DesignerItem>))
            {
                throw new InvalidCastException("The target must be a List of DesignerItem");
            }
            this.designerItems = (List<DesignerItem>)parameters[0];

            Redo();
        }

        public void Redo()
        {
            foreach (DesignerItem di in this.designerItems)
            {
                if (!this.designerCanvas.Children.Contains(di))
                {
                    this.designerCanvas.Children.Add(di);
                    this.designerCanvas.SelectionService.SelectItem(di);
                }
            }
        }

        public void Undo()
        {
            foreach (DesignerItem di in this.designerItems)
            {
                if (this.designerCanvas.Children.Contains(di))
                    this.designerCanvas.Children.Remove(di);
            }
            this.designerCanvas.SelectionService.ClearSelection();
        }

    }
}
